<?php

namespace App\Http\Controllers\Admin;

use App\Model\Cate;
use App\Model\Goods;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Config;

use App\Http\Requests\GoodsRequest;

class GoodsController extends Controller
{
    /**
     * 商品列表
     */
    public function index(){
        $cateInfo = $this->getCateInfo();
        return view('admin/goods/index',['cateInfo' => $cateInfo]);
    }
    /**
     * 商品新增
     */
    public function add(){
        $cateInfo = $this->getCateInfo();
        return view('admin/goods/add',["cateInfo" => $cateInfo]);
    }
    /**
     * 商品新增结果
     */
    public function addRes(GoodsRequest $request){
        $data = $request->all();
        $goods = new Goods();
        $goods->cate_id = $data['cate_id'];
        $goods->name = $data['name'];
        $goods->description = $data['description'];
        $goods->price = $data['price'];
        $goods->reprice = $data['reprice'];
        $goods->detail = $data['detail'];
        $goods->remark = $data['remark'];
        $goods->num = $data['num'];
        $goods->integral = $data['integral'];
        $goods->thumb_img = $this->upImg($request,'thumb');
        $goods->main_img = $this->upImg($request,'pic');
        $res = $goods->save();
        if($res){
            return redirect('admin/goods-list')->with('success','商品新增成功');
        }else{
            return back()->with('error','商品新增失败');
        }
    }

    /**
     * @param Request $request
     * 商品修改
     */
    public function update($id){
        $goods = new Goods();
        $goodsInfo = $goods->find($id);
        $cateInfo = $this->getCateInfo();
        return view('admin/goods/update',[
            'goodsInfo' => $goodsInfo,
            'cateInfo' => $cateInfo
        ]);
    }

    /**
     * @param Request $request
     * @return mixed
     * 商品修改结果
     */
    public function updateRes(Request $request){
        $goods = new Goods();
        $data = $request->all();
        $goodsInfo = $goods->find($data['id']);
        $goodsInfo->name = $data['name'];
        $goodsInfo->cate_id = $data['cate_id'];
        $goodsInfo->reprice = $data['reprice'];
        $goodsInfo->price = $data['price'];
        $goodsInfo->num = $data['num'];
        $goodsInfo->integral = $data['integral'];
        $goodsInfo->description = $data['description'];
        $goodsInfo->remark = $data['remark'];
        $goodsInfo->detail = $data['detail'];
        if(!empty($data['pic'])){
            @unlink('.'.$goodsInfo->main_img);
            $goodsInfo->main_img = $this->upImg($request,'pic');
        }
        if(!empty($data['thumb'])){
            @unlink('.'.$goodsInfo->thumb_img);
            $goodsInfo->thumb_img = $this->upImg($request,'thumb');
        }
        $res = $goodsInfo->save();
        if($res){
            return redirect('/admin/goods-list')->with("success","修改成功");
        }else{
            return back()->with("error",'修改失败，请稍后再试');
        }
    }

    /**
     * @param Request $request
     * 异步删除商品
     */
    public function delete(Request $request){
        $goods = new Goods();
        $id = $request->input("goods_id");
        $res = $goods->where('id',$id)->update(['status' => '2']);
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }
    /**
     * 图片上传
     */
    private function upImg($request,$name){
        if($request->hasFile($name)){
            $path = Config::get('app.goodsImgPath');
            $fileName = time().rand(10000,999999).'.'.$request->file($name)->getClientOriginalExtension();
            $request->file($name)->move($path, $fileName);
            return substr($path.$fileName,1);
        }
    }
    

    /**
     * @param Request $request
     * 异步修改商品上下架
     */
    public function changeStatus(Request $request){
        $goods = new Goods();
        $id = $request->input("goods_id");
        $status = $request->input("status");
        $res = $goods->where('id',$id)->update(['status'=>$status]);
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }
}
